// pages/bills/bills.js
var utils = require('../../../utils/util.js') 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hide: true,
    hide1: true,
    click1: false,
    click2: false,
    filterList1:[
      '全部',
      '支付',
      '返佣',
      '分佣'
    ],
    filterList2: [
      '全部',
      '提现',
      '购买产品',
    ],
    select:0,
    status:0,
    data:[],
    tabKey:0,
    page:1,
    ym:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getbill();
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      ym:"",
      data: [],
      page: 1,
    });
    this.getbill();
  },
  //上拉加载
  onReachBottom: function () {
    this.getbill();
  },
  getbill:function(){
    var that = this;
    var userinfo = wx.getStorageSync("userinfo");
    var token = userinfo.wap_token;
    var data = {
      url: "/v1/userbill/lists",
      token: token,
      type: that.data.select,
      status: that.data.status,
      ym: that.data.ym,
      page: that.data.page,
      limit: 100,
    };
    utils.request(data).then(function (res) {
      that.setData({
        ym: res.data.ym
      });
      let new_data = [];
      let now = new Date().getFullYear();
      let k = res.data.list.length;
      for (now; now > 2017; now--) {
        let y = 0;
        let year = new Array();

        for (let j = 0; j < k; j++) {
          let time = res.data.list[j];
          var pastY = time.create_time_name.slice(0, 4);
          if (pastY == now) {
            year[y++] = time
          }
        }
        let s = year.length;
        for (let m = 12; m > 0; m--) {
          let month = new Array();
          let income = 0;
          let expenditure = 0;
          let num = 0;
          for (let j = 0; j < s; j++) {
            let time2 = year[j];
            let pastM = time2.create_time_name.slice(5, 7);
            if (m == pastM) {
              month[num++] = time2;
              if (year[j].type != undefined) {
                if (year[j].type >= 200) {
                  expenditure += parseFloat(year[j].show_money)
                } else {
                  income += parseFloat(year[j].show_money)
                }
              }
              var a = pastM;
            }
          }
          expenditure = expenditure.toFixed(2);
          income.toFixed(2);
          if (month != '') {
            month.unshift({ year: now, month: a, income: income, expenditure, expenditure });
            new_data.push.apply(that.data.data, month);
          }
        }
      };
      that.setData({
        data: that.data.data.concat(new_data)
      })
      wx.stopPullDownRefresh();
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  expenditure:function(){
    this.setData({
      hide: !this.data.hide,
      hide1: true,
      click1: !this.data.click1,
      click2: false
    })
  },
  income:function(){
    this.setData({
      hide1: !this.data.hide1,
      hide: true,
      click2: !this.data.click2,
      click1: false
    })
  },
  //赛选收入 类型
  incomeTab:function(e){  
    let k = e.currentTarget.dataset.key;
    var data = {};
    switch (k) {
      case 0:
        data = {
          select: 0,
          tabKey: 0,
          status: -1,
          data: [],
          hide:true
        };
        break;
      case 1:
        data = {
          select: 202,
          status: -1,
          tabKey: 1,
          data: [],
          hide: true
        };
        break;
      case 2:
        data = {
          select: 200,
          tabKey: 2,
          status: -1,
          data: [],
          hide: true
        };
        break;
    }
    this.setData({
      ym: ""
    });
    this.setData(data);
    this.getbill();
  },
  //支出赛选
  expenditureTab:function(e){
    let k = e.currentTarget.dataset.key;
    var data = {};
    switch (k) {
      case 0:
        data = {
          select: 0,
          tabKey: 0,
          status: 1,
          data: [],
          hide1: true
        };
        break;
      case 1:
        data = {
          select: 100,
          status: 1,
          tabKey: 1,
          data: [],
          hide1: true
        };
        break;
      case 2:
        data = {
          select: 104,
          status: 1,
          tabKey: 2,
          data: [],
          hide1: true
        };
        break;
      case 3:
        data = {
          select: 103,
          status: 1,
          tabKey: 3,
          data: [],
          hide1: true
        };
        break;
    };
    this.setData({
      ym: ""
    });
    this.setData(data);
    this.getbill();
  }
})